﻿using Factory.MesEntity.CustomerDatas;
using Factory.WarehouseManagement.SNDatas.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.WarehouseManagement.SNDatas
{
    public class SNDataAppService:ApplicationService, ISNDataAppService
    {
        IRepository<SNData,int> _snDataRepository;

        public SNDataAppService(IRepository<SNData,int> snDataRepository)
        {
            _snDataRepository = snDataRepository;
        }

        public (List<SNData> units, int total) GetSNDataList(string snName, int pageIndex = 1, int pageSize = 10)
        {
            var list = _snDataRepository.GetListAsync().Result.ToList();

            int total = list.Count();
            var pagedUnits = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return (pagedUnits, total);
        }

        public int CreateAsync(SNData sndata)
        {
            var list = _snDataRepository.GetListAsync(x => x.SNName == sndata.SNName).Result;

            // 检查是否已存在该单位名称
            if (list.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var customerData = _snDataRepository.InsertAsync(new SNData
            {
                SNCode=sndata.SNCode,
                SNName=sndata.SNName,
                Models=sndata.Models,
                Unit=sndata.Unit,
                BatchCode = sndata.BatchCode,
                SNNum=sndata.SNNum,
            });
            return 1;

        }
        public int UpdateAsync(SNDataUpdateDto input)
        {
            var customer = _snDataRepository.FindAsync(x => x.Id == input.Id).Result;
            if (customer == null)
            {
                return 0;
            }
            var isDuplicate = _snDataRepository.FindAsync(x => x.SNCode == input.SNCode || x.SNName == input.SNName && x.Id != input.Id).Result;
            if (isDuplicate != null)
            {
                return -1; // 返回特定的状态码以表示重复
            }
            customer.SNCode = input.SNCode;
            customer.SNName = input.SNName;
            customer.Models = input.Models;
            customer.Unit = input.Unit;
            customer.BatchCode = input.BatchCode;
            customer.SNNum = input.SNNum;

            _snDataRepository.UpdateAsync(customer);
            return 1;
        }

        public async Task<int> DeleteAsync(int id)
        {
            var customer = await _snDataRepository.FindAsync(x => x.Id == id);
            if (customer != null)
            {
                await _snDataRepository.DeleteAsync(customer);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public SNData GetSNDataById(int id)
        {
            var customer = _snDataRepository.GetAsync(id).Result;
            return customer;
        }
    }
}
